package com.tengju.bff.interfaces.covercharge.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tengju.bff.interfaces.covercharge.dto.QueryCustomDetailReqDTO;
import com.tengju.bff.interfaces.covercharge.dto.ServiceCustomDetailDTO;
import com.tengju.bff.interfaces.covercharge.entity.po.ServiceCustomDetail;
import com.tengju.bff.interfaces.covercharge.entity.vo.AdminServiceCustomQueryVo;
import com.tengju.bff.interfaces.covercharge.enums.ProfitTypeEnum;
import com.tengju.bff.interfaces.covercharge.mapper.ServiceCustomDetailMapper;
import com.tengju.bff.interfaces.covercharge.service.IServiceCustomDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhaoyi
 * @since 2020-02-24
 */
@Slf4j
@Service
public class ServiceCustomDetailServiceImpl extends ServiceImpl<ServiceCustomDetailMapper, ServiceCustomDetail>
        implements IServiceCustomDetailService {

    @Autowired
    private ServiceCustomDetailMapper serviceCustomDetailMapper;


    @Override
    public Page<AdminServiceCustomQueryVo> queryPageByIdCode(Page page, String idCode, String orderId, String otherId,
                                                             String des, String profitType, String startDate, String endDate) {
        page.setRecords(addOtherId(serviceCustomDetailMapper
                .queryPageByIdCode(page, idCode, orderId, otherId, des, profitType, startDate, endDate)));
        return page;
    }

    /**
     * @return java.util.List<com.tengju.bff.interfaces.covercharge.entity.vo.AdminServiceCustomQueryVo>
     * @Author xx
     * @Description 填充其它信息id
     * @Date: 2020/7/16 17:24
     * @Param []
     **/
    public List<AdminServiceCustomQueryVo> addOtherId(List<AdminServiceCustomQueryVo> list) {
        for (AdminServiceCustomQueryVo adminServiceCustomQueryVo : list) {
            //1.根据收益支出类型判断
            int type = adminServiceCustomQueryVo.getProfitType();
            if (type == ProfitTypeEnum.pf_in.getCode() || type == ProfitTypeEnum.pf_out.getCode()) {
                String otherId = "订单id:" + adminServiceCustomQueryVo.getOrderId() + ";商品id:" +
                        (adminServiceCustomQueryVo.getGoodsId() != null ? adminServiceCustomQueryVo.getGoodsId() : "无");
                adminServiceCustomQueryVo.setOtherId(otherId);
                continue;
            }
            if (type == ProfitTypeEnum.jy_in.getCode() || type == ProfitTypeEnum.jy_out.getCode()) {
                String otherId = "订单id:" + adminServiceCustomQueryVo.getOrderId() + ";商品id:" +
                        (adminServiceCustomQueryVo.getGoodsId() != null ? adminServiceCustomQueryVo.getGoodsId() : "无");
                adminServiceCustomQueryVo.setOtherId(otherId);
                continue;
            }
            if (type == ProfitTypeEnum.ls_withdraw.getCode() || type == ProfitTypeEnum.ls_withdraw_refund.getCode()) {
                adminServiceCustomQueryVo.setOtherId("乐税提现id:" + adminServiceCustomQueryVo.getTableId());
                continue;
            }
            if (type == ProfitTypeEnum.self_withdraw.getCode() || type == ProfitTypeEnum.self_withdraw_refund
                    .getCode()) {
                adminServiceCustomQueryVo.setOtherId("自认证提现id:" + adminServiceCustomQueryVo.getTableId());
                continue;
            }
            if (type == ProfitTypeEnum.return_pf.getCode()) {
                String otherId = "退款id:" + (adminServiceCustomQueryVo.getReturnId() == null ? "无" : adminServiceCustomQueryVo.getReturnId()) +
                        ";订单id:" + (adminServiceCustomQueryVo.getOrderId() == null ? "无" : adminServiceCustomQueryVo.getOrderId());
                adminServiceCustomQueryVo.setOtherId(otherId);
                continue;
            }
            if (type == ProfitTypeEnum.return_jy.getCode()) {
                String otherId = "退款id:" + (adminServiceCustomQueryVo.getReturnId() == null ? "无" : adminServiceCustomQueryVo.getReturnId())+
                        ";订单id:" + (adminServiceCustomQueryVo.getOrderId() == null ? "无" : adminServiceCustomQueryVo.getOrderId());
                adminServiceCustomQueryVo.setOtherId(otherId);
            }
        }
        return list;
    }

    /**
     * 根据IDCode和表id获取客戶明細
     */
    @Override
    public List<ServiceCustomDetailDTO> getListFromIdCodeAndTableIdList(QueryCustomDetailReqDTO req) {
        List<ServiceCustomDetailDTO> lists = this.baseMapper.getListFromIdCodeAndTableIdList(req.getIdCode(),
                req.getTableIdList(), req.getOrderId(), Objects.isNull(req.getProfitType()) ? null : req.getProfitType().getCode());
        return lists;
    }

    /**
     * 获取客戶明細
     */
    @Override
    public List<ServiceCustomDetailDTO> getCustomDetailList(QueryCustomDetailReqDTO req) {
        List<ServiceCustomDetailDTO> lists = this.baseMapper.getCustomDetailList(req.getIdCode(), req.getTableIdList(),
                req.getOrderId(), req.getDes(), Objects.isNull(req.getProfitType()) ? null : req.getProfitType().getCode(),
                req.getCreatedAt(), req.getUpdatedAt());
        return lists;
    }
}
